.TH std::pair::operator= 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::pair::operator= \- std::pair::operator=

.SH Synopsis
   pair& operator=( const pair& other );                (until
                                                        C++20)
   constexpr pair& operator=( const pair& other         (since
   );                                                   C++20)
   constexpr const pair& operator=( const pair&     \fB(2)\fP (since
   other ) const;                                       C++23)
   template< class U1, class U2 >                               (until
   pair& operator=( const pair<U1, U2>& other                   C++20)
   );
   template< class U1, class U2 >                               (since
   constexpr pair& operator=( const pair<U1,                    C++20)
   U2>& other );
   template< class U1, class U2 >                               (since
   constexpr const pair& operator=( const               \fB(4)\fP     C++23)
   pair<U1, U2>& other ) const;
                                                                        (since
   pair& operator=( pair&& other ) noexcept(/*                          C++11)
   see below */);                                                       (until
                                                \fB(1)\fP                     C++20)
   constexpr pair& operator=( pair&& other )                            (since
   noexcept(/* see below */);                                           C++20)
   constexpr const pair& operator=( pair&&          \fB(3)\fP         \fB(6)\fP     (since
   other ) const;                                                       C++23)
                                                                                (since
   template< class U1, class U2 >                                               C++11)
   pair& operator=( pair<U1, U2>&& p );                                         (until
                                                        \fB(5)\fP                     C++20)
   template< class U1, class U2 >                                               (since
   constexpr pair& operator=( pair<U1, U2>&& p                                  C++20)
   );
   template< class U1, class U2 >                               \fB(7)\fP             (since
   constexpr const pair& operator=( pair<U1,                            \fB(8)\fP     C++23)
   U2>&& p ) const;
   template< pair-like P >                                              \fB(9)\fP     (since
   constexpr pair& operator=( P&& u );                                          C++23)
   template< pair-like P >                                                      (since
   constexpr const pair& operator=( P&& u )                             \fB(10)\fP    C++23)
   const;

   Replaces the contents of the pair.

   1) Copy assignment operator. Replaces the contents with a copy of the contents of
   other.

   The assignment operator is implicitly declared. Using this assignment
   operator makes the program ill-formed if either T1 or T2 is a          \fI(until C++11)\fP
   const-qualified type, or a reference type, or a class type with an
   inaccessible copy assignment operator, or an array type of such class.
   This overload is defined as deleted if either
   std::is_copy_assignable<T1>::value or                                  \fI(since C++11)\fP
   std::is_copy_assignable<T2>::value is false.

   2) Copy assignment operator for const-qualified operand.
   This overload participates in overload resolution only if
   std::is_copy_assignable_v<const T1> and std::is_copy_assignable_v<const T2> are both
   true.
   3) Assigns other.first to first and other.second to second.

   This overload participates in overload resolution only if
   std::is_assignable<T1&, const U1&>::value and std::is_assignable<T2&,  \fI(since C++11)\fP
   const U2&>::value are both true.

   4) Assigns other.first to first and other.second to second.
   This overload participates in overload resolution only if std::is_assignable_v<const
   T1&, const U1&> and std::is_assignable_v<const T2&, const U2&> are both true.
   5) Move assignment operator. Replaces the contents with those of other using move
   semantics.
   This overload participates in overload resolution only if
   std::is_move_assignable<T1>::value and std::is_move_assignable<T2>::value are both
   true.
   6) Move assignment operator for const-qualified operand.
   This overload participates in overload resolution only if std::is_assignable_v<const
   T1&, T1> and std::is_assignable_v<const T2&, T2> are both true.
   7) Assigns std::forward<U1>(p.first) to first and std::forward<U2>(p.second) to
   second.
   This overload participates in overload resolution only if std::is_assignable<T1&,
   U1>::value and std::is_assignable<T2&, U2>::value are both true.
   8) Assigns std::forward<U1>(p.first) to first and std::forward<U2>(p.second) to
   second.
   This overload participates in overload resolution only if std::is_assignable_v<const
   T1&, U1> and std::is_assignable_v<const T2&, U2> are both true.
   9) Assigns std::get<0>(std::forward<P>(u)) to first and
   std::get<1>(std::forward<P>(u)) to second.
   This overload participates in overload resolution only if
     * std::same_as<std::remove_cvref_t<P>, std::pair> is false,
     * std::remove_cvref_t<P> is not a specialization of std::ranges::subrange,
     * std::is_assignable_v<T1&, decltype(std::get<0>(std::forward<P>(p)))> is true,
       and
     * std::is_assignable_v<T1&, decltype(std::get<1>(std::forward<P>(p)))> is true.
   10) Assigns std::get<0>(std::forward<P>(u)) to first and
   std::get<1>(std::forward<P>(u)) to second.
   This overload participates in overload resolution only if
     * std::same_as<std::remove_cvref_t<P>, std::pair> is false,
     * std::remove_cvref_t<P> is not a specialization of std::ranges::subrange,
     * std::is_assignable_v<const T1&, decltype(std::get<0>(std::forward<P>(p)))> is
       true, and
     * std::is_assignable_v<const T1&, decltype(std::get<1>(std::forward<P>(p)))> is
       true.

.SH Parameters

   other - pair of values to replace the contents of this pair
   p     - pair of values of possibly different types to replace the contents of this
           pair
   u     - pair-like object of values to replace the contents of this pair
.SH Type requirements
   -
   T1 must meet the requirements of CopyAssignable from U1. \fI(until C++11)\fP
   -
   T2 must meet the requirements of CopyAssignable from U2. \fI(until C++11)\fP

.SH Return value

   *this

.SH Exceptions

   1-4) May throw implementation-defined exceptions.
   5)
   noexcept specification:
   noexcept(

       std::is_nothrow_move_assignable<T1>::value &&
       std::is_nothrow_move_assignable<T2>::value

   )
   6-10) May throw implementation-defined exceptions.

.SH Example


// Run this code

 #include <cstddef>
 #include <iomanip>
 #include <iostream>
 #include <utility>
 #include <vector>

 template<class Os, class T>
 Os& operator<<(Os& os, const std::vector<T>& v)
 {
     os << '{';
     for (std::size_t t = 0; t != v.size(); ++t)
         os << v[t] << (t + 1 < v.size() ? ", " : "");
     return os << '}';
 }

 template<class Os, class U1, class U2>
 Os& operator<<(Os& os, const std::pair<U1, U2>& pair)
 {
     return os << '{' << pair.first << ", " << pair.second << '}';
 }

 int main()
 {
     std::pair<int, std::vector<int>> p{1, {2}}, q{2, {5, 6}};

     p = q; // (1) operator=(const pair& other);
     std::cout << std::setw(23) << std::left
               << "(1) p = q;"
               << "p: " << p << "     q: " << q << '\\n';

     std::pair<short, std::vector<int>> r{4, {7, 8, 9}};
     p = r; // (3) operator=(const pair<U1, U2>& other);
     std::cout << std::setw(23)
               << "(3) p = r;"
               << "p: " << p << "  r: " << r << '\\n';

     p = std::pair<int, std::vector<int>>{3, {4}};
     p = std::move(q); // (5) operator=(pair&& other);
     std::cout << std::setw(23)
               << "(5) p = std::move(q);"
               << "p: " << p << "     q: " << q << '\\n';

     p = std::pair<int, std::vector<int>>{5, {6}};
     p = std::move(r); // (7) operator=(pair<U1, U2>&& other);
     std::cout << std::setw(23)
               << "(7) p = std::move(r);"
               << "p: " << p << "  r: " << r << '\\n';
 }

.SH Output:

 \fB(1)\fP p = q;             p: {2, {5, 6}}     q: {2, {5, 6}}
 \fB(3)\fP p = r;             p: {4, {7, 8, 9}}  r: {4, {7, 8, 9}}
 \fB(5)\fP p = std::move(q);  p: {2, {5, 6}}     q: {2, {}}
 \fB(7)\fP p = std::move(r);  p: {4, {7, 8, 9}}  r: {4, {}}

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to          Behavior as published              Correct behavior
   LWG 885  C++98      missing heterogeneous copy assignment    added (as overload \fB(3)\fP)
                       pair::operator= was unconstrained and
   LWG 2729 C++11      might                                    constrained
                       result in unnecessary undefined behavior

.SH See also

   operator= assigns the contents of one tuple to another
             \fI(public member function of std::tuple<Types...>)\fP

.SH Category:
     * conditionally noexcept
